'''
Created on 2009-12-12

@author: wangyongtao
'''
#!/usr/bin/python

import os
import re
import optparse
import logging
from logging import handlers
import urllib
import smtplib
import datetime
from xml.dom.minidom import parse, parseString

#logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')

COL_ID_BOSSTOWN = 38
COL_ID_BRAINSTORM = 19
COL_ID_FINANCE =12
COL_ID_CHINAMANAGER = 14
COL_ID_WORKINGLIFE = 40

def main():
    usage = "usage: %prog [options]"
    parser = optparse.OptionParser(usage)
    parser.add_option("-o", "--output", dest="output", help="output directory", metavar="OUTPUT")
    parser.add_option("-c", "--columnid", dest="colid", help="column id", metavar="COLID")
    options,args = parser.parse_args()
    if not options.output:
        parser.error("Must specify filename and output")
    if not options.colid:
        options.colid = COL_ID_BOSSTOWN
    print 'Column ID:'
    print options.colid
    if options.output:
        logging.info("Output:%s"%options.output)
        if not os.path.isdir(options.output):
            logging.error('outpput is not valid directory')
            return
    url = 'http://cbnchannel.appspot.com/colshowlist.xml?col=%d'
    listurl = url%int(options.colid)
    print listurl
    file_log(listurl)
    download_list(options.output, listurl)
    file_log('Column show download finished ')    
    #send_email()
            
def download_list(output, url):
    try:
        urlhandler = urllib.urlopen(url)
        xmldata = urlhandler.read()
        if not xmldata:
            logging.error('download xml media list failed')
            return
        dom = parseString(xmldata)
        videos = dom.documentElement.getElementsByTagName('video')
        feedback = dom.documentElement.getAttribute('feedback')
        logging.info('feedback url:%s'%feedback)
        mms = None
        for media in videos:
            href = media.getAttribute('href')
            key = media.getAttribute('key')
            logging.info('media url:%s'%href)
            result = recordmms(href, output)
            template ='%(url)s?key=%(key)s&result=%(suc)d'
            if result:
                url = template%{'url':feedback, 'key':key, 'suc': 1}
            else:
                url = template%{'url':feedback, 'key':key, 'suc': 0}
            logging.info('feedback url:%s'%url)
            mms = href
            urllib.urlopen(url)
        return mms
    except IOError, e:
        msg = 'download met error with url:%s'%url
        logging.error(msg)
        file_log(msg)
                
def recordmms(url, target):
    result = False
    splits = url.split('/')
    filename = splits[len(splits)-1]
    logging.info('video filename:%s'%filename)
    pattern = '^(\d+){1}'
    match = re.match(pattern, filename)
    if match:
        datestr = match.groups(0)[0]
        logging.info('video date:%s'%datestr)
        targetfolder = os.path.join(target, datestr)
        logging.info('output directory:%s'%targetfolder)
        if not os.path.exists(targetfolder):
            cmd = 'mkdir %s'%targetfolder
            retcode = call_command(cmd)
            logging.info('command ret code:%d'%retcode)
            if retcode == 0:
                logging.info('create folder succeed')
        target = os.path.join(targetfolder, filename)
        
        retcode = record_mms(url, target)
        if retcode == 0:
            result = True
        else:
            result = False
        return result
        
def record_mms(url, target):
    mencodercmd = "%(mencoder)s %(input)s -ovc copy -oac copy -o %(output)s"%{'mencoder': '~/bin/mencoder', 'input':url, 'output':target}
    logging.info(mencodercmd)
    print mencodercmd
    file_log(mencodercmd)
    #return 0
    return call_command(mencodercmd)
    #cmd = 'ls'
    #return call_command(cmd)
    
def convert_mms(input, target):
    options = "-vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256 \
       -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \
       -me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \
       -flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \
           -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10\
       -qmax 51 -qdiff 4"
    #ffmpeg -y -i "$infile" -an -pass 1 -threads 2 $options "$tmpfile"
    #ffmpeg -y -i "$infile" -acodec libfaac -ar 44100 -ab 96k -pass 2 -threads 2 $options "$tmpfile"
    
    ffmpegcmd = "%(ffmpeg)s -i %(input)s -f mp4 "

def send_email():
    smtpserver = 'smtp.sohu.com'
    AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
    smtpuser = 'atia@sohu.com'  # for SMTP AUTH, set SMTP username here
    smtppass = '8651397'  # for SMTP AUTH, set SMTP password here
  
    RECIPIENTS = ['yongtao.wang@gmail.com']
    SENDER = 'atia@sohu.com'
    mssg = 'downloading video done!'
  
    session = smtplib.SMTP(smtpserver)
    if AUTHREQUIRED:
        session.login(smtpuser, smtppass)
    smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)
    if smtpresult:
        errstr = ""
        for recip in smtpresult.keys():
            errstr = """Could not delivery mail to: %s
              Server said: %s
              %s
              
              %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
        raise smtplib.SMTPException, errstr

def file_log(msg):
    filename = 'downloader.log'
    if not os.path.exists(filename):
        FILE = open(filename,"w+")
    else:
        FILE = open(filename,"a+")
    FILE.write(msg)
    now = datetime.datetime.now()
    ts = ' @ %(year)d-%(month)d-%(day)d %(hour)d-%(min)d'%{'year':now.year, 'month':now.month, 'day':now.day, 'hour':now.hour,'min':now.minute}
    FILE.write(ts)
    FILE.write('\n')
    FILE.close()

def call_command(command):
    try:
        import subprocess
        process = subprocess.Popen(command.split(' '),
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
        process.communicate()
        return process.returncode
    except ImportError, e:
        logging.info('does not support subprocess module:%s')
        return os.system(command)
       
if __name__ == "__main__":
    main()
    